<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/13
 * Time: 14:30
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;

class Jurisdiction extends AdminBase
{
    /**
     * 权限分组
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $list = Db::name('admin_role')->field('id, title, status, create_time')->select();
            return view('role', [
                'list'=>$list
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function add()
    {
        $chk = check_juri(request()->controller(), 'add', $this->_juri);
        if ($chk['status'])
        {
            $group = Db::name('admin_nav_group')
                ->field('id, title')
                ->order('sort asc')
                ->select();
            return view('add', [
                'group'=>$group
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }

    }

    public function doAdd()
    {
        if (request()->isPost())
        {
            $data                 = [];
            $data['title']        = input('param.title');
            $data['jurisdiction'] = json_encode(input('param.juri/a'));
            $data['nav']          = json_encode(input('param.nav/a'));
            $data['create_time']  = time();
            $res = Db::name('admin_role')->insertGetId($data);
            return $res ? true : false;
        }
    }

    public function edit()
    {
        $chk = check_juri(request()->controller(), 'edit', $this->_juri);
        if ($chk['status'])
        {
            $id   = intval(input('param.id'));
            $info = Db::name('admin_role')
                ->field('id, title, jurisdiction, nav')
                ->where('id', $id)
                ->find();
            $info['jurisdiction'] = json_decode($info['jurisdiction'], true);
            $info['nav']          = json_decode($info['nav'], true);
            $group = Db::name('admin_nav_group')
                ->field('id, title')
                ->order('sort asc')
                ->select();
            return view('edit', [
                'info'=>$info,
                'group'=>$group
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }

    }

    public function doEdit()
    {
        if (request()->isPost())
        {
            $id                   = intval(input('param.id'));
            $data                 = [];
            $data['title']        = input('param.title');
            $data['jurisdiction'] = json_encode(input('param.juri/a'));
            $data['nav']          = json_encode(input('param.nav/a'));
            $data['update_time']  = time();
            $res = Db::name('admin_role')->where('id', $id)->update($data);
            return $res ? true : false;
        }
    }

    public function del()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $id                   = intval(input('param.id'));
                $row                  = Db::name('admin')->where('level', $id)->find();
                if (!empty($row))
                {
                    return ['stauts'=>false, 'msg'=>'该分组下有管理员，无法删除！'];
                }
                $res = Db::name('admin_role')->where('id', $id)->delete();
                return $res ? ['status'=>true, 'msg'=>''] : ['status'=>false, 'msg'=>'删除失败'];
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }
}